package com.phonebills.service.impl;

import java.util.List;

import com.phonebills.dao.UserDao;
import com.phonebills.dao.impl.UserDaoImpl;
import com.phonebills.service.UserService;
import com.phonebills.vo.Bills;
import com.phonebills.vo.Page;
import com.phonebills.vo.User;

/**
 * 用户业务逻辑实现
 * @author LIU
 * @version 1.0 2017-10-27
 */
public class UserServiceImpl implements UserService {

	/**
	 * 创建数据访问对象
	 */
	UserDao userDao = new UserDaoImpl();
	
	/**
	 * 用户登陆
	 *TODO
	 *LIU
	 * @param user
	 * @return boolean
	 *下午4:03:47
	 */
	@Override
	public boolean login(User user) {
		// TODO Auto-generated method stub
		if(userDao.login(user) > 0){
			return true;
		}
		return false;
	}

	
	/**
	 * 获取所有账单的总记录数
	 *TODO
	 *LIU
	 * @param page
	 * @return
	 *下午4:16:37
	 */
	@Override
	public int getAllCounts() {
		// TODO Auto-generated method stub
		return userDao.getAllCounts();
	}


	/**
	 * 分页显示所有账单
	 *TODO
	 *LIU
	 * @param page
	 * @return
	 *下午4:20:11
	 */
	@Override
	public List<Bills> showAll(Page page) {
		// TODO Auto-generated method stub
		return userDao.showAll(page);
	}


	/**
	 * 获取当前用户
	 *TODO
	 *LIU
	 * @param no
	 * @return
	 *下午5:39:25
	 */
	@Override
	public User getCurrUser(String no) {
		// TODO Auto-generated method stub
		return userDao.getCurrUser(no);
	}


	/**
	 * 计算总价
	 *TODO
	 *LIU
	 * @param type
	 * @param bills
	 * @return
	 *下午6:44:03
	 */
	@Override
	public double calTotalAmount(char type, Bills bills) {
		// TODO Auto-generated method stub
		double totalAmount = 0;
		double messageNum = 0;
		double telPeriod = 0;
		double internetTraffic = 0;
		
		if(type == 'A'){
			messageNum = (bills.getMessageNum() > 100) ? (bills.getMessageNum() - 100)*0.1 : 0;
			telPeriod = (bills.getTelPeriod() > 20) ? (bills.getTelPeriod() - 20)*0.1 : 0;
			internetTraffic = (bills.getInternetTraffic() > 50) ? (bills.getInternetTraffic() - 50)*0.1 : 0;
			
			totalAmount = internetTraffic + telPeriod + messageNum + 5;
		}else{
			
			messageNum = (bills.getMessageNum() > 100) ? (bills.getMessageNum() - 100)*0.1 : 0;
			telPeriod = (bills.getTelPeriod() > 50) ? (bills.getTelPeriod() - 100)*0.15 : 0;
			internetTraffic = (bills.getInternetTraffic() > 100) ? (bills.getInternetTraffic() - 100)*0.1 : 0;
			
			totalAmount = internetTraffic + telPeriod + messageNum + 10;
		}
		
		return totalAmount;
	}
	
	
}
